package com.foreknow.demo11;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * Map集合
 */
public class MapDemo {
    public static <Iterators> void main(String[] args) {
        Person p1 = new Person();
        p1.setAge(1);
        Person p2 = new Person();
        p2.setAge(2);
        Person p3 = new Person();
        p3.setAge(3);
        //创建一个map容器
        Map<Integer,Person> maps = new HashMap<>();
        //向Map容器添加元素
        maps.put(1,p1);
        maps.put(2,p2);
        maps.put(3,p3);
        //从Map容器中获取某一个元素
        Person pp = maps.get(1);
        System.out.println(pp.getAge());
        Person pp2 = maps.get(2);
        System.out.println(pp2.getAge());
        //遍历Map集合
        Set<Map.Entry<Integer,Person>> s = maps.entrySet();//因为Map中没有迭代器，所以通过调用EntrySet()获取到Set集合
        Iterator<Map.Entry<Integer,Person>> ss =  s.iterator();
        while(ss.hasNext()){
            Map.Entry entry = ss.next();
            Integer key = (Integer) entry.getKey();
            Person ppp = (Person) entry.getValue();
            System.out.println(key+"----"+ppp.getAge());
        }
    }
}
